<?php

class Constant_IndexController extends Zend_Controller_Action
{
	protected $_object;

	public function init()
	{
		$this->_object = new Admin_Model_Constant ();
	}

	//Liet ke danh sach hang so.
	public function indexAction()
	{
		//lấy tham số
		$request = $this->_request;
		$search_keyword = $request->getParam ( 'search_keyword', '' );
		
		//các cột cần lấy dữ liệu
		$select = " 
		SELECT
			`constant_id`, 
			`constant_name`, 
			`constant_note`, 
			`constant_code`, 
			`constant_order`,
			`constant_constant_group_code`,
			`constant_group_name`
		FROM tbl_constant LEFT JOIN tbl_constant_group ON constant_constant_group_code=constant_group_code ";
		
		//thiết lập điều kiện lấy dữ liệu
		$where = " WHERE `constant_status`='1' ";
		if ($search_keyword != "")
			$where .= " AND (constant_code LIKE '%$search_keyword%' OR constant_content LIKE '%$search_keyword%' OR constant_note LIKE '%$search_keyword%' OR constant_name LIKE '%$search_keyword%') ";
		
		//thiết lập thứ tự lấy dữ liệu
		$order = $this->_helper->common->getOrderString ( array('constant_order ASC'), array ('constant_constant_group_code ASC' ) );
		
		//lấy dữ liệu
		$query = $select . $where . $order;
		$data = $this->_object->queryBase ( $query );
		
		//phân trang
		$this->_helper->common->paginate ( $data );
		
		//truyền dữ liệu ra view
		$this->view->search_keyword = $search_keyword;
	}

	//Them hang so.
	public function insertAction()
	{
		//Load dong css va javascript files.
		$this->view->headScript ()->appendFile ( JS . 'ckeditor/ckeditor.js' );
		
		//Tao form
		$form = new Constant_Form_Constant_Insert ($this);
		
		//Xet dieu kien Insert record.
		if ($this->_request->isPost ())
		{
			if ($form->isValid ( $this->_request->getPost () ))
			{
				$values = $form->getValues ();
				$constant_model = new Admin_Model_Constant ( $values );
				$constant_model->insertBase ();
				$this->_helper->flashMessenger ( 'Thêm thành công!' );
				if ($this->_request->getParam ( 'adminAction' ) == 'save-and-close')
				{
					$this->_helper->redirector ( 'index', 'index', 'constant', array () );
				}
				else if ($this->_request->getParam ( 'adminAction' ) == 'save-and-new')
				{
					$this->_helper->redirector ( 'insert', 'index', 'constant', array () );
				}
			}
		}
		
		//Truyen tham so ra VIEW.
		$this->view->form = $form;
	}

	//Cap nhat hang so.
	public function updateAction()
	{
		//lay tham so
		$id=$this->_request->getParam("id");
		
		//Load dong css va javascript files.
		$this->view->headScript ()->appendFile ( JS . 'ckeditor/ckeditor.js' );
		
		//lay do tuong can cap nhat
		$this->_object->findBase($id);
		
		//Tao form
		$form = new Constant_Form_Constant_Update ($this);
		
		//Xet dieu kien Insert record.
		if ($this->_request->isPost ())
		{
			if ($form->isValid ( $this->_request->getPost () ))
			{
				$values = $form->getValues ();
				$constant_model = new Admin_Model_Constant ( $values );
				$constant_model->updateBase ($id);
				$this->_helper->flashMessenger ( 'Cập nhật thành công!' );
				if ($this->_request->getParam ( 'adminAction' ) == 'apply')
				{
					$this->_helper->redirector ( 'update', 'index', 'constant', array ('id'=>$id) );
				}
				if ($this->_request->getParam ( 'adminAction' ) == 'save-and-close')
				{
					$this->_helper->redirector ( 'index', 'index', 'constant', array () );
				}
				else if ($this->_request->getParam ( 'adminAction' ) == 'save-and-new')
				{
					$this->_helper->redirector ( 'insert', 'index', 'constant', array () );
				}
			}
		}
		else 
		{
			$form->populate($this->_object->toArray());
		}
		
		//Truyen tham so ra VIEW.
		$this->view->form = $form;
	}

	//lưu thứ tự sắp xếp của các dòng
	public function saveOrderAction()
	{
		$this->_helper->action->saveOrder ( $this->_object, 'constant_order' );
	}

	//xóa 1 hoặc nhiều dòng
	public function deleteAction()
	{
		$this->_helper->action->delete ( $this->_object );
	}

	//Lay thong tin hang so.
	public function detailAction()
	{
		//Tat layout 
		$this->_helper->getHelper ( 'ViewRenderer' )->setNoRender ( true );
		
		//lay tham so.
		$constant_id = $this->_request->getParam ( 'constant_id', '' );
		$constant_code = $this->_request->getParam ( 'constant_code', '' );
		$constant_content=$this->_object->fetchConstantContent($constant_code);
		echo $constant_content;
	}
}